import dayjs from "dayjs"
/**
 * 显示消息提示框
 * @param content 提示的标题
 */
export function toast(content) {
  uni.showToast({
    icon: "none",
    title: content,
  });
}

/**
 * 显示模态弹窗
 * @param content 提示的标题
 */
export function showConfirm(content, showCancel = true) {
  return new Promise((resolve, reject) => {
    uni.showModal({
      title: "提示",
      content: content,
      showCancel: showCancel,
      cancelText: "取消",
      confirmText: "确定",
      success: function (res) {
        resolve(res);
      },
    });
  });
}

/**
 * 参数处理
 * @param params 参数
 */
export function tansParams(params) {
  let result = "";
  for (const propName of Object.keys(params)) {
    const value = params[propName]
    var part = encodeURIComponent(propName) + "="
    if (value !== null && value !== "" && typeof(value) !== "undefined") {
      if (typeof value === 'object') {
        for (const key of Object.keys(value)) {
          if (value[key] !== null && value[key] !== "" && typeof(value[key]) !== 'undefined') {
            let params = propName + '[' + key + ']'
            var subPart = encodeURIComponent(params) + "="
            result += subPart + encodeURIComponent(value[key]) + "&"
          }
        }
      } else {
        result += part + encodeURIComponent(value) + "&";
      }
    }
  }
  return result
}

// 时间戳转时间字符串
export function formatDate(date, format) {
  // 日期不存在，则返回空
  if (!date) {
    return ''
  }
  // 日期存在，则进行格式化
  if (format === undefined) {
    format = 'YYYY-MM-DD HH:mm:ss'
  }
  return dayjs(date).format(format)
}
export function formatRichText(nodes = "") {
  return nodes.replace(/<p><\/p>/g, '<br/>')
    .replace(/(style="(.*?)")|(width="(.*?)")|(height="(.*?)")/ig, '')
    .replace(/<pre/g, '<pre style="max-width: 100%;max-height: 100%;margin: 10px 0;padding: 15px;overflow: auto;background-color: #f1f2f3;border-radius: 3px;color: #666;font-family: monospace;font-size: 14px;font-weight: 400;line-height: 16px;word-wrap: break-word;white-space: pre-wrap;"')
    .replace(/<blockquote/g, '<blockquote style="margin: 0 0 10px;padding: 15px 20px;background-color: #f1f2f3;border-left: 5px solid #ccc;color: #666;font-style: italic;"')
    .replace(/\<img/g, '<img style="width: 100%; height: auto;display: block;"')
    .replace(/\pre/g, 'p');
}